package controller

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/olivere/elastic"
)

type Student struct {
	Name    string `json:"name"`
	Age     int    `json:"age"`
	Married bool   `json:"married"`
}

func (s *Student) run() {
	fmt.Printf("%s在跑...", s.Name)
}

func (s *Student) wang() {
	fmt.Printf("%s在汪...", s.Name)
}

func (s *Student) EsList(c *gin.Context) {
	//1.初始化连接，得到一个client
	client, err := elastic.NewClient(elastic.SetURL("http://180.76.172.93:9200"))
	if err != nil {
		panic(err)
	}
	fmt.Println("connect to es success")
	p1 := Student{Name: "Rion", Age: 22, Married: false}
	//链式操作
	put1, err := client.Index().
		Index("Student"). //Index表数据库
		Type("go"). //Type是表
		BodyJson(p1). //把一个go语言的对象转换为json格式
		Do(context.Background())
	if err != nil {
		panic(err)
	}
	fmt.Printf("Indexed Student %s to index %s,type %s\n", put1.Id, put1.Index, put1.Type)
}
